package dao

import (
	"Cgo/front/models"
	"Cgo/global"
)

type userDao struct{}

var UserDao = new(userDao)

// 用户登录
func (userDao) Login(user *models.Users) error {
	if r := global.DB.Where(user).First(user); r.RowsAffected > 0 {
		return nil
	} else {
		return r.Error
	}
}

// 用户注册
func (userDao) Register(user models.Users) error {
	if r := global.DB.Create(&user); r.RowsAffected > 0 {
		return nil
	} else {
		return r.Error
	}
}

// 获取用户好友
func (userDao) GetFriends(user *models.Users) ([]models.Users, error) {
	var friends []models.Users
	//select * from users where id in (select friend_id from user_friends where user_id = 1)
	if r := global.DB.Table("users").Where("id in (select friend_id from user_friend where user_id = ?)", user.Id).Find(&friends); r.RowsAffected > 0 {
		return friends, nil
	} else {
		return nil, r.Error
	}
}

// 获取用户信息
func (userDao) GetUserMsg(user *models.Users) (models.Users, error) {
	if r := global.DB.Where(user).First(user); r.RowsAffected > 0 {
		return *user, nil
	} else {
		return *user, r.Error
	}
}

// 获取用户好友申请
func (userDao) GetFriendApply(user *models.Users) ([]models.UserFriendApply, error) {
	var apply []models.UserFriendApply
	if r := global.DB.Raw("SELECT *, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') AS apply_time FROM v_user_apply WHERE apply_id = ?", user.Id).Scan(&apply); r.RowsAffected > 0 {
		return apply, nil
	} else {
		return nil, r.Error
	}
}

// 处理好友的申请
func (userDao) HandleApplication(userId string, friendId string, isAccept string) bool {
	var deleteApply = func() {
		global.DB.Where("user_id = ? and apply_id = ?", userId, friendId).Delete(&models.Apply{})
	}
	global.Logger.Info("userId:", userId, "friendId:", friendId, "isAccept:", isAccept)
	//根据用户传来的信息判断是否接收好友
	if isAccept == "1" && global.DB.Where(" user_id = ? and apply_id = ?", userId, friendId).Find(&models.Apply{}).RowsAffected > 0 {
		if r := global.DB.Create(&models.UserFriend{UserId: userId, FriendId: friendId}); r.RowsAffected > 0 {
			deleteApply()
			return true
		} else {
			return false
		}
	}
	global.Logger.Info("userId:", userId, "friendId:", friendId, "isAccept:", isAccept)
	return false
}

// 删除好友
func (userDao) DeleteFriend(userId, friengId string) bool {
	if r := global.DB.Where("user_id = ? and friend_id = ?", userId, friengId).Delete(&models.UserFriend{}); r.RowsAffected > 0 {
		return true
	} else {
		return false
	}
}

// 请求添加好友
func (userDao) RequestAddFriend(userFriendApply models.UserFriendApply) bool {
	//判断是否是好友关系
	// if r := global.DB.Create(&models.Apply{UserId: userId, ApplyId: friendId}); r.RowsAffected > 0 {
	// 	return true
	// } else {
	// 	return false
	// }
	if r := global.DB.Table("apply").Create(&models.Apply{
		UserId:       userFriendApply.UserId,
		ApplyId:      userFriendApply.FriendId,
		ApplyContent: userFriendApply.ApplyContent}); r.RowsAffected > 0 {
		return true
	} else {
		return false
	}
}

// 修改用户信息
func (userDao) UpdateUserInfo(user models.Users) bool {
	if r := global.DB.Model(&user).Updates(user); r.RowsAffected > 0 {
		return true
	} else {
		return false
	}
}

// 搜索用户
func (userDao) SearchUser(user *models.Users) ([]models.Users, error) {
	var users []models.Users
	if r := global.DB.Where("name like ?", "%"+user.Name+"%").Find(&users); r.RowsAffected > 0 {
		return users, nil
	} else {
		return nil, r.Error
	}
}

// 获取用户的个人信息
func (userDao) GetUserInfo(user *models.Users) (*models.Users, error) {
	if r := global.DB.Where(user).First(user); r.RowsAffected > 0 {
		return user, nil
	} else {
		return user, r.Error
	}
}
